package main

import (
	"bytes"
	"encoding/json"
	"fmt"
)

type Metric struct {
	Name  string `json:"name,omitempty"`
	Value int64  `json:"value,omitempty"`
}

func (u *Metric) UnmarshalJSON(data []byte) error {
	type AliasMetric Metric
	t := &struct {
		Value float64 `json:"value"`
		*AliasMetric
	}{
		Value:       float64(u.Value),
		AliasMetric: (*AliasMetric)(u),
	}

	if err := json.Unmarshal(data, t); err != nil {
		return err
	}
	u.Value = int64(t.Value)
	return nil
}

func main1() {
	var in = []*Metric{
		{"vv", 12},
		{"vtz", 13},
		{"ss", 82},
	}

	// out, err := json.Marshal(in)

	var body []byte
	var out = bytes.NewBuffer(body)
	err := json.NewEncoder(out).Encode(in)
	if err != nil {
		fmt.Printf("%+v", out)
		return
	}
	fmt.Printf("%+v", out)
}

func main() {
	var metric Metric
	var in = []byte(`{"name": "tq", "value": 13.14}`)
	err := json.Unmarshal(in, &metric)
	if err != nil {
		panic(err)
	}
	fmt.Println(metric)
}
